//
//  FOutlineView.m
//  SolarSeek
//
//  Created by Erick Fejta on 05/03/23.
//  Copyright 2005 Erick Fejta. All rights reserved.
//

#import "FOutlineView.h"


@implementation FOutlineView
- (id)init {
	if(self = [super init]) {
		_root = [[FOutlineViewArrayElement alloc] init];
		[super setDataSource:[[FOutlineViewDataSource alloc] initWithRoot:_root]];
	}
	return self;
}


- (id)initWithFrame:(NSRect)frameRect {
	if(self = [super initWithFrame:frameRect]) {
		_root = [[FOutlineViewArrayElement alloc] init];
		[super setDataSource:[[FOutlineViewDataSource alloc] initWithRoot:_root]];
	}
	return self;
}

- (void)dealloc {
	[_root release];
	[_dataSource release];
	[super dealloc];
}

- (void)awakeFromNib {

	if(_root==nil)
		_root = [[FOutlineViewArrayElement alloc] init];
	[super setDataSource:[[FOutlineViewDataSource alloc] initWithRoot:_root]];

	NSEnumerator *e = [[super tableColumns] objectEnumerator];
	NSTableColumn *tc;
	while(tc = [e nextObject]) {
		if([[tc identifier] isKindOfClass:[NSString class]]) {
			[_dataSource setTableColumn:tc performsSelector:NSSelectorFromString([tc identifier])];
		}
	}
}

- (void)setDataSource:(id)s { 
	[NSException raise:@"InvalidMethod" format:@"Do not call setDataSource with FOutlineView"];
}

- (id<FOutlineViewElement>)root { return _root; }
- (void)setRoot:(id<FOutlineViewElement>)root { 
	[_root release]; 
	_root = [root retain]; 
	[_dataSource setRoot:root];
	[self reloadData];
}

- (NSArray *)selectedObjects {
	NSIndexSet *e = [super selectedRowIndexes];
	unsigned int n = [e firstIndex];
	NSMutableArray *ma = [NSMutableArray array];
	do {
		[ma addObject:[self itemAtRow:n]];
	} while((n = [e indexGreaterThanIndex:n]) != NSNotFound);
	
	return ma;
}
		
@end
